package com.ruoyi.web.controller.training;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.live.base.domain.TaskCatalog;
import com.ruoyi.live.base.service.ITaskCatalogService;
import com.ruoyi.live.configuration.SessionInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模块管理Controller
 *
 * @author guoyou
 * @date 2023-01-22
 */
@RestController
@RequestMapping("/training/module")
public class TaskCatalogController extends BaseController
{
    @Autowired
    private ITaskCatalogService taskCatalogService;

    /**
     * 查询模块管理列表
     */
    @PreAuthorize("@ss.hasPermi('training:module:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaskCatalog taskCatalog)
    {
        startPage();
        List<TaskCatalog> list = taskCatalogService.selectTaskCatalogList(taskCatalog);
        return getDataTable(list);
    }

    /**
     * 导出模块管理列表
     */
    @PreAuthorize("@ss.hasPermi('training:module:export')")
    @Log(title = "模块管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskCatalog taskCatalog)
    {
        List<TaskCatalog> list = taskCatalogService.selectTaskCatalogList(taskCatalog);
        ExcelUtil<TaskCatalog> util = new ExcelUtil<TaskCatalog>(TaskCatalog.class);
        util.exportExcel(response, list, "模块管理数据");
    }

    /**
     * 获取模块管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:module:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(taskCatalogService.getById(id));
    }

    /**
     * 新增模块管理
     */
    @PreAuthorize("@ss.hasPermi('training:module:add')")
    @Log(title = "模块管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskCatalog taskCatalog)
    {
        taskCatalog.setCreateTime(System.currentTimeMillis());
        taskCatalog.setCreateUser(SessionInfo.getUserId());
        taskCatalog.setState(1);
        return toAjax(taskCatalogService.save(taskCatalog));
    }

    /**
     * 修改模块管理
     */
    @PreAuthorize("@ss.hasPermi('training:module:edit')")
    @Log(title = "模块管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskCatalog taskCatalog)
    {
        return toAjax(taskCatalogService.updateById(taskCatalog));
    }

    /**
     * 删除模块管理
     */
    @PreAuthorize("@ss.hasPermi('training:module:remove')")
    @Log(title = "模块管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        return toAjax(taskCatalogService.removeByIds(list));
    }
}
